<?php
	class Database {
		function __construct() {
			if (!defined('CONFIGURED')) {
				throw new Exception('Not configured.');
				exit;
			}
			
			mysql_pconnect(DB_HOST, DB_USER, DB_PASS);
			mysql_select_db(DB_NAME);
		}
		
		private function commaSeparate($vals, $backticks = true) {
			if (!is_array($vals)) $vals = array($vals);
			$a = array();
			foreach ($vals as $val) {
				if (count($a) > 0) $a[] = ', ';
				if ($backticks) $a[] = "`$val`";
				else $a[] = $val;
			}
			
			return $a;
		}
		
		public function count($h) {
			return mysql_numrows($h);
		}
		
		public function delete($field, $ids, $tbls) {
			$q = array();
			$q[] = 'DELETE FROM ';
			$q = array_merge($q, $this -> commaSeparate($tbls));
			
			if ($ids !== NULL) {
				$q[] = " WHERE `$field` IN (";
				$q = array_merge($q, $this -> commaSeparate($ids, false));
				$q[] = ')';
			}
			
			mysql_query(implode('', $q)) or die(mysql_error());
			return mysql_affected_rows();
		}
		
		public function deleteWhere($tbls, $where = false) {
			$q = array();
			$q[] = 'DELETE FROM ';
			$q = array_merge($q, $this -> commaSeparate($tbls));
			
			if ($where) {
				$q[] = "WHERE $where";
			}
		}
		
		public function getId() {
			return mysql_insert_id();
		}
		
		public function insert($rows, $tbl, $ignore = false) {
			$ignore = ($ignore) ? ' IGNORE' : '';
			
			$q = array();
			$q[] = "INSERT$ignore INTO `$tbl` (";
			
			if (!is_array(current($rows))) $rows = array($rows);
			
			$keys = array_keys(current($rows));
			$q = array_merge($q, $this -> commaSeparate($keys));
			$q[] = ') VALUES (';
			
			$s = false;
			foreach ($rows as $row) {
				if ($s) $q[] = ', (';
				else $s = true;
				
				$z = false;
				foreach ($row as $col) {
					$col = addslashes($col);
					if ($z) $q[] = ', ';
					else $z = true;
					
					$q[] = "'$col'";
				}
				
				$q[] = ')';
			}
			
			mysql_query(implode('', $q)) or die(implode('', $q) . mysql_error());
			return mysql_affected_rows();
		}
		
		public function row($id) {
			return mysql_fetch_assoc($id);
		}
		
		// TODO: Maybe implement whereGroup for less sql-specific select
		public function select(
			$fields, $from,
			$where = false, $special = false, $order = false,
			$dir = false, $limit = false
		) {
			if (!is_array($fields)) $fields = array($fields);
			if (!is_array($from)) $from = array($from);
			
			$bt = '';
			if (!$special) $bt ='`';
			
			$q = array();
			$alias = '';
			$q[] = 'SELECT ';
			
			foreach($fields as $field) {
				unset($alias);
				if (count($q) > 1) $q[] = ', ';
				
				if (is_array($field)) {
					$alias = $field[1];
					$field = $field[0];
				}
				
				if ($field === '*') $q[] = '*';
				else $q[] = "{$bt}$field{$bt}" . ((isset($alias)) ? " AS `$alias`" : '');
			}
			
			$q[] = ' FROM ';
			$q = array_merge($q, $this -> commaSeparate($from));
			
			if ($where) {
				$q[] = " WHERE $where";
			}
			
			if ($order) {
				$q[] = " ORDER BY `$order` $dir";
			}
			
			if ($limit) {
				$q[] = " LIMIT $limit";
			}
			
			$result = mysql_query(implode('', $q));
			if (!$result) {
					echo implode('', $q);
					die(mysql_error());
			}
			
			return $result;
		}
		
		public function query($string) {
			$result = mysql_query($string);
			if (!$result) die(mysql_error());
			return $result;
		}
	}
?>